<?php

namespace app\admin\controller\auth;

use app\common\library\Auth;
use brand\build\Columns;
use brand\logic\BrandHelper;
use brand\page\Form;
use brand\page\Table;
use brand\tools\Tree;
use brand\tools\Random;
use app\admin\model\AuthGroup;
use app\admin\controller\Base;
use app\admin\model\AuthGroupAccess;
use think\facade\Validate;

/**
 * 管理员管理.

 */
class Admin extends Base
{
    /**
     * @var \app\admin\model\Admin
     */
    protected $model = null;
    protected $curdName = 'admin';
    protected $pk = 'id';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Admin();


    }

    public function db_reset($db)
    {
        return $this->model;
    }


    public function table_init()
    {
        Table::$visible_edit = "_options";
        Table::$visible_delete = "_options";
    }

    public function form_before_build($raw )
    {

        $rule_select =[];

        $groupList = AuthGroup::where('id', 'in', $this->auth->getChildrenGroupIds(true))->field('id,name')->select()->toArray();

        foreach ($groupList as $groupdatum) {
            $rule_select[] = ['label' => $groupdatum['name'],'value' => $groupdatum['id']];
        }
        $group =   [
            'title' =>  '所属分组',
            'form'  =>  [
                'method'  =>  'checkbox',
                'options'=>$rule_select,
                'validate'=>true
            ]
        ];
        $columns = BrandHelper::arraySplice(Columns::getConfig(),'nickname','group',$group);
        Columns::setConfig($columns);


        if (isset($raw['id'])) $raw['group'] = ( new AuthGroupAccess() )->where(['uid'=>$raw['id']])->column('group_id');
        if (isset($raw['password'])) unset($raw['password']);
        if (!isset($raw['status'])) $raw['status'] = 'normal';

        return $raw;
    }

    public function data_after_select($count, $list): array
    {
        foreach ($list as $key=>$datum) {
            $list[$key]['_options'] = $datum['id'] != 1;
        }
        return [$count, $list];
    }


    public function submit_save_before($params)
    {
        //修改了登陆密码
        if (isset($params['password']) && $params['password']){
            $params['salt'] = Random::build();
            $params['password'] = Auth::instance()->getEncryptPassword($params['password'],$params['salt']);
        }else{
            if (isset($params['password'])) unset($params['password']);
        }
        return $params;
    }

    public function submit_save_after($params,$id){

        ( new AuthGroupAccess() )->setAccess($id,$params['group'] ?? []);
    }

    public function profile()
    {
        if ($this->request->isAjax()){

            $params = $this->request->param();
            $raw = $this->model->where(['id'=>$this->auth->id])->find($params);
            $raw->allowField(['nickname','password','avatar','email','salt'])->save($params);
            $this->success('操作成功');
        }

        $raw = $this->model->find($this->auth->id);
        $raw->password = "";

        //构造Form表单
        $columns = Columns::getConfig();
        unset($columns['status']);
        unset($columns['username']);

        $columns_arr = Form::buildForm($columns,$raw);

        Form::$SurfaceForm->columns($columns_arr);

        return Form::$SurfaceForm->view();
    }



}
